package com.gxa.pingan.service.impl;


import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.mapper.ETCMapper;
import com.gxa.pingan.pojo.ETC;
import com.gxa.pingan.service.ETCService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lijianmin
 * @date 2021/10/26  11:37:48
 */
@Service("etcService")
@Transactional(rollbackFor = {Throwable.class}, propagation = Propagation.REQUIRED)
public class ETCServiceImpl implements ETCService {

    @Autowired
    private ETCMapper etcMapper;

    /**
     * ETC申请列表查询的方法,搜索条件为空则进行分页查询,搜索条件不为空则进行条件查询+分页查询
     *
     * @param name 搜索条件
     * @param page 页码
     * @param size 每页记录数
     * @return 返回查询到的集合
     */
    @Override
    public List<ETC> getList(String name, Integer page, Integer size) throws BusinessException {
        if (page != null) {
            if (page <= 0) {
                throw new BusinessException(ExceptionCode.PAGE_ERROR);
            } else {
                page = (page - 1) * size;
            }
        }

        if (size != null) {
            if (size <= 0) {
                throw new BusinessException(ExceptionCode.SIZE_ERROR);
            }
        }

        return etcMapper.getList(name, page, size);
    }

    /**
     * ETC申请列表计数的方法,搜索条件为空则进行全查询计数,搜索条件不为空则进行条件查询计数
     *
     * @param name 搜索条件
     * @return 返回查询到的总数
     */
    @Override
    public Integer getListCount(String name) throws BusinessException {
        return etcMapper.getList(name, null, null).size();
    }


}
